<html>
	<body>
		
		<form method="get">
		<font size=5>Super Math Machine:</font><br>
		<input type="text" name="inputExpression" />
		<input type="submit" value="Math it!" />
		</form>
		
		<?php
			if ($_GET["inputExpression"] != NULL) {
				calculate($_GET["inputExpression"]);
			}
				
			// calculates the inpute equation
			function calculate( $expression ) {

  	 	 		// fail if expression has weird characters
  	 	 		if (preg_match('/[^0-9\+\-\*\/\(\)\. ]/', $expression)) {
  	 	 			echo "Invalid input expression: " . $expression;
  	 	 		}else {
					// check for werid expression and handle the error gracefully
					if(FALSE === @eval('$results = '.$expression.';')){
						echo "Invalid input expression: " . $expression;
					}else{
  	 	 				echo $expression . "=" . $results;
					}
  	 	 		}
			}
		?>
	
	</body>
</html>
